Skip to content

Conversation

@iabdalkader
Copy link

@iabdalkader iabdalkader commented Nov 17, 2025

This PR optimizes flash and memory usage, saving up to ~80KBs flash and ~50KBs RAM on some boards, by disabling the shell for production builds, CPP and using a unified heap for Zephyr and sketches. The saved memory is used to increase mbedtls heap to support cert loading, and the remaining added to main heap. For Portenta, ~100KBs should be available to user sketches.

Changes:

  • Disable libc heap (set to 0) and use a kernel heap for Zephyr and sketches.
  • Disable CPP. This saves flash and memory used by libc heap.
  • Disable the shell for production builds. This saves about 70KBs FLASH and ~47KBs RAM.
  • Add debug config overlay to easily build debug images.

To build debug images use: ./extra/build.sh <board> --debug
The build includes extra/debug.conf which enables:

  • Debug mode with assertions and verbose output
  • Debug-level logging (level 4)
  • Stack overflow detection
  • Thread monitoring
  • Debug optimizations (-Og)
  • Enable the shell

@iabdalkader iabdalkader requested a review from pillo79 November 18, 2025 08:12
@iabdalkader iabdalkader marked this pull request as draft November 21, 2025 07:36
@iabdalkader
Copy link
Author

FYI @pillo79 @pennam for some reason enabling log level=4 (debug for everything) causes a hardfault on Portenta-H7. It shouldn't be a stack overflow issue, but I tried increasing all stacks anyway and it doesn't help. That said, it is very convenient and I have been using it locally, instead of adding debug options manually to conf files, so I will get it fixed somehow and let you know.

@iabdalkader iabdalkader marked this pull request as ready for review November 21, 2025 14:14
@iabdalkader
Copy link
Author

Actually, it turns out that it's just some log thread overflow, fixable by increasing stack sizes. However,, I decided to Not enable debug logs globally, because we don't really have extra memory for that and it makes it much harder to see the debug log you're looking for.

Add debug config fragment to easily build debug images.

Usage: ./extra/build.sh <board> --debug

The build includes extra/debug.conf which enables:

- Debug mode with assertions and verbose output
- Debug-level logging (level 4)
- Stack overflow detection
- Thread monitoring
- Debug optimizations (-Og)
- Enable shell.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
@iabdalkader iabdalkader changed the title extra: Add debug config overlay. misc: Optimize flash and memory usage. Nov 26, 2025
The created sketch log structures depends on the config:

CONFIG_LOG (IMMEDIATE/DEFERRED): creates log_const_sketch struct.
CONFIG_LOG_RUNTIME_FILTERING (implied by CONFIG_SHELL): creates log_dynamic_sketch struct.
CONFIG_LOG_MODE_IMMEDIATE + CONFIG_SHELL: creates both.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
Replace the libc's malloc, free etc.. with wrappers that redirect
memory allocation Zephyr's kernel heap functions (k_malloc/k_free).

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
This saves about 70KBs FLASH and ~47KBs RAM. The saved memory
is used to increase mbedtls heap to support cert loading, and
the remaining added to main heap.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant